const {
  schoolList,
  majorPageList,
  searchList
} = require('../../api/api')
const {
  returnLabel
} = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    historyList: [],
    hotList: ['哲学', '经济学', '软件工程', '北京大学', '清华大学'],
    pageNo: 1,
    pageSize: 20,
    options: [],
    name: '',
    showList: true,
    isFocus: false,
    type: 'other',
    schoolList: [],
    schoolTotal: 0,
    majorList: [],
    majorTotal: 0,
    tabList: ['综合', '院校', '专业'],
    currentIndex: 0,
    settingSchool: {
      padding: '32rpx',
      width: '750rpx',
    },
    itemStyle: {
      width: '750rpx',
      padding: '32rpx',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type || 'other',
      name: options.keywords || ''
    })
    if (options.type === 'school') {
      this.setData({
        historyList: wx.getStorageSync('schoolOptions') || [],
      })
    } else if (options.type === 'major') {
      this.setData({
        historyList: wx.getStorageSync('majorOptions') || [],
      })
    }else{
      this.setData({
        historyList:wx.getStorageSync('otherOptions') || [],
      })
    }
  },
  getList() {
    this.setData({
      pageNo: 1,
      list: [],
      showList: false,
      currentIndex: 0,
      schoolList: [],
      majorList: [],
      schoolTotal: 0,
      majorTotal: 0
    })
    const {
      type
    } = this.data
    if (type == 'school') {
      this.getSchoolList()
    } else if (type == 'major') {
      this.getMajorList()
    } else {
      this.getSearchList()
    }
  },
  changeIndex(e) {
    const {
      index
    } = e.currentTarget.dataset
    if(index==this.data.currentIndex)return
    this.setData({
      currentIndex: index,
      pageNo:1,
      list:[]
    })
    if (index == 1) {
      this.getSchoolList()
    } else if (index == 2) {
      this.getMajorList()
    }
  },
  async getSearchList() {
    const {
      name
    } = this.data
    const params = {
      keyword: name
    }
    const {
      result
    } = await searchList(params)
    const {
      schoolPage,
      specialtyPage
    } = result
    const schoolList = schoolPage ? schoolPage.records : []
    const majorList = specialtyPage ? specialtyPage.records : []
    schoolList.map((item) => {
      item['filter'] = returnLabel(item)
    })
    this.setData({
      schoolTotal: schoolPage && schoolPage.total,
      majorTotal: specialtyPage && specialtyPage.total,   
      ['schoolList[0]']: schoolList,
      ['majorList[0]']: majorList,
    })
  },
  async getSchoolList() {
    const {
      type,
      pageNo,
      pageSize,
      name
    } = this.data
    const params = {
      pageNo,
      pageSize,
      name
    }
    const {
      result
    } = await schoolList(params)
    if (result.records.length == 0) {
      return
    }
    result.records.map((item) => {
      item['filter'] = returnLabel(item)
    })
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },
  async getMajorList() {
    const {
      type,
      pageNo,
      pageSize,
      name
    } = this.data
    const params = {
      level: 3,
      pageNo,
      pageSize,
      name
    }
    const {
      result
    } = await majorPageList(params)
    if (result.records.length == 0) {
      return
    }
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },
  inputChange(e) {
    this.setData({
      name: e.detail.value
    })
    if (e.detail.value.length === 0) {
      this.setData({
        showList: true
      })
    }
  },
  search() {
    this.getList()
    const {
      historyList,
      name,
    } = this.data
    const array = historyList
    if (name !== '') {
      if (array.length > 20) {
        array.splice(19, 1)
      }
      if (array.length == 0) {
        array.unshift(name)
        this.saveKeywords(array)
        this.setData({
          historyList: array
        })
        return
      }
      array.map((item, index) => {
        if (item == name) {
          array.splice(index, 1)
        }
      })
      array.unshift(name)
      this.saveKeywords(array)
      this.setData({
        historyList: array
      })

    }

  },
  clear() {
    this.setData({
      pageNo: 1,
      list: [],
      name: '',
      showList: true,
      currentIndex: 0,
      schoolList: [],
      majorList: [],
      schoolTotal: 0,
      majorTotal: 0,
      isFocus: false
    })
  },
  keywords(e) {
    this.setData({
      name: e.currentTarget.dataset.value
    })
    this.setData({
      pageNo: 1,
      showList: false
    })
    this.getList()
  },
  hotkeywords(e) {
    this.setData({
      name: e.currentTarget.dataset.value
    })
    this.setData({
      pageNo: 1,
      showList: false,
    })
    this.getList()
    const {
      historyList,
      name
    } = this.data
    const array = historyList
    if (name !== '') {
      if (array.length > 20) {
        array.splice(19, 1)
      }
      if (array.length == 0) {
        array.unshift(name)
        this.saveKeywords(array)
        this.setData({
          historyList: array
        })
        return
      }
      array.map((item, index) => {
        if (item == name) {
          array.splice(index, 1)
        }
      })
      array.unshift(name)
      this.saveKeywords(array)
      this.setData({
        historyList: array
      })

    }
  },
  saveKeywords(list) {
    const {
      type
    } = this.data
    if (type === 'school') {
      wx.setStorageSync('schoolOptions', list)
    } else if (type === 'major') {
      wx.setStorageSync('majorOptions', list)
    }else{
      wx.setStorageSync('otherOptions', list)
    }
  },
  focus(e) {
    this.setData({
      isFocus: true
    })
  },
  blur(e) {
    const {
      name
    } = this.data
    if (name.length > 0) {
      this.setData({
        isFocus: true
      })
      return
    }
    this.setData({
      isFocus: false
    })
  },
  goback() {
    wx.navigateBack()
  },
  onReachBottom: function () {
    const {
      type,
      list,
      pageSize,
      currentIndex
    } = this.data
    if (type == 'other' && currentIndex == 0) return
    const len = list.length - 1
    if (list[len].length < pageSize) {
      return
    }
    if (type === 'school'||currentIndex==1) {
      this.getSchoolList()
    } else if(type=='major'||currentIndex==2){
      this.getMajorList()
    }
  },
  collect(e) {
    const list=JSON.parse(JSON.stringify(e.detail.list))
    this.setData({
      list: e.detail.list,
      ['schoolList[0]']:list[0].splice(0,3)
    })
  }
})